Java EE 7 Recipes by Josh Juneau

Java EE 7 Recipes by Josh Juneau

Author:Josh Juneau
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


7-8. Utilizing Java Objects for Database Access

Problem

Your application works with an underlying database for storing and retrieving data. You would prefer to code your business logic using Java objects, rather than working directly with JDBC and SQL for performing database activities.

Solution

Create a data access object (DAO) for each database table that will be used to perform the mundane JDBC and SQL work. Within the DAO, create façade methods that accept Java objects to represent a single record of data for the database table for which the DAO has been created. Use the Java objects to pass record data to and from the DAO, while the DAO breaks the objects apart and utilizes the data fields within standard SQL statements.

The following class excerpts demonstrate a data access object for the AUTHOR database table, which is used for storing book author data (a main method has been included merely for testing purposes within this DAO).

Note

For the full source listing, please refer to the org.javaeerecipes.chapter07.dao.AuthorDAO class, located in the JavaEERecipes NetBeans project. Repetitive portions of the sources (finally blocks) have been removed from the following listing for brevity.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.